var _o131;
var cc__decorate = __decorate;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.READ_FILE_TYPE = undefined;
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var r;
(function (e) {
  e[e.DATA_URL = 0] = "DATA_URL";
  e[e.TEXT = 1] = "TEXT";
  e[e.BINARY = 2] = "BINARY";
  e[e.ARRAYBUFFER = 3] = "ARRAYBUFFER";
})(r = exports.READ_FILE_TYPE || (exports.READ_FILE_TYPE = {}));
var def_UploadTexture = _o131 = /* */function (_cc$Component31) {
  function _ctor() {
    babel_classCallCheck(this, _ctor);
    return _callSuper(this, _ctor, arguments);
  }
  babel_inherits(_ctor, _cc$Component31);
  return babel_createClass(_ctor, [{
    key: "openFile",
    value: function openFile(e) {
      var _this140 = this;
      this.openLocalFile("", function (t) {
        _this140.readLocalFile(t, r.DATA_URL, function (t) {
          if (t) {
            var _n69 = t.toString();
            _this140.base64ToSpriteFrame(_n69, function (t) {
              e.spriteFrame = t;
            });
          }
        });
      });
    }
  }, {
    key: "openLocalFile",
    value: function openLocalFile(e, t) {
      var n = document.getElementById("file_input");
      if (!n) {
        (n = document.createElement("input")).id = "file_input";
        n.setAttribute("id", "file_input");
        n.setAttribute("type", "file");
        n.setAttribute("class", "fileToUpload");
        n.style.opacity = "0";
        n.style.position = "absolute";
        n.setAttribute("left", "-999px");
        document.body.appendChild(n);
      }
      e = e || ".*";
      n.setAttribute("accept", e);
      n.onchange = function () {
        var e = n.files;
        if (e && e.length > 0) {
          var _n70 = e[0];
          t && t(_n70);
        }
      };
      n.click();
    }
  }, {
    key: "readLocalFile",
    value: function readLocalFile(e, t, n) {
      var _o132 = new FileReader();
      _o132.onload = function () {
        if (n) {
          if (_o132.readyState == FileReader.DONE) {
            n(_o132.result);
          } else {
            n(null);
          }
        }
      };
      switch (t) {
        case r.DATA_URL:
          _o132.readAsDataURL(e);
          break;
        case r.TEXT:
          _o132.readAsText(e);
          break;
        case r.BINARY:
          _o132.readAsBinaryString(e);
          break;
        case r.ARRAYBUFFER:
          _o132.readAsArrayBuffer(e);
      }
    }
  }, {
    key: "base64ToTexture2D",
    value: function base64ToTexture2D(e, t) {
      if (e) {
        var _n71 = new Image();
        _n71.onload = function () {
          var e = new cc.Texture2D();
          e.initWithElement(_n71);
          e.handleLoadedTexture();
          t && t(e);
        };
        _n71.onerror = function () {};
        if (undefined !== e.startsWith && e.startsWith("data:image")) {
          _n71.src = e;
        } else {
          _n71.src = "data:image/png;base64," + e;
        }
      } else {
        t && t(null);
      }
    }
  }, {
    key: "base64ToSpriteFrame",
    value: function base64ToSpriteFrame(e, t) {
      this.base64ToTexture2D(e, function (e) {
        if (e) {
          var _n72 = new cc.SpriteFrame(e);
          t && t(_n72);
        } else {
          t && t(null);
        }
      });
    }
  }], [{
    key: "getInstance",
    value: function getInstance() {
      null == this.instance && (this.instance = new _o131());
      return this.instance;
    }
  }]);
}(cc.Component);
def_UploadTexture.instance = null;
def_UploadTexture = _o131 = cc__decorate([ccp_ccclass], def_UploadTexture);
exports.default = def_UploadTexture;